home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH3 / SRC / PALEDIT.FRM < prev    next >
Text File  |  1997-01-03  |  42KB  |  1,449 lines

  1. VERSION 4.00
  2. Begin VB.Form PalEditForm 
  3.    Caption         =   "PalEdit"
  4.    ClientHeight    =   5805
  5.    ClientLeft      =   1305
  6.    ClientTop       =   780
  7.    ClientWidth     =   7020
  8.    Height          =   6495
  9.    Left            =   1245
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   388
  12.    ScaleMode       =   0  'User
  13.    ScaleWidth      =   468
  14.    Top             =   150
  15.    Width           =   7140
  16.    Begin VB.PictureBox ColorSwatch 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   2280
  19.       Left            =   4560
  20.       Picture         =   "PALEDIT.frx":0000
  21.       ScaleHeight     =   2220
  22.       ScaleWidth      =   2400
  23.       TabIndex        =   15
  24.       Top             =   2505
  25.       Width           =   2460
  26.    End
  27.    Begin VB.PictureBox SystemColors 
  28.       AutoRedraw      =   -1  'True
  29.       Height          =   2460
  30.       Left            =   4560
  31.       Picture         =   "PALEDIT.frx":0446
  32.       ScaleHeight     =   160
  33.       ScaleMode       =   3  'Pixel
  34.       ScaleWidth      =   160
  35.       TabIndex        =   14
  36.       Top             =   0
  37.       Width           =   2460
  38.    End
  39.    Begin VB.PictureBox ColorBox 
  40.       BorderStyle     =   0  'None
  41.       Height          =   975
  42.       Left            =   0
  43.       ScaleHeight     =   65
  44.       ScaleMode       =   3  'Pixel
  45.       ScaleWidth      =   468
  46.       TabIndex        =   4
  47.       Top             =   4830
  48.       Width           =   7020
  49.       Begin VB.HScrollBar BlueScroll 
  50.          Enabled         =   0   'False
  51.          Height          =   255
  52.          LargeChange     =   16
  53.          Left            =   885
  54.          Max             =   255
  55.          TabIndex        =   7
  56.          Top             =   720
  57.          Width           =   6090
  58.       End
  59.       Begin VB.HScrollBar GreenScroll 
  60.          Enabled         =   0   'False
  61.          Height          =   255
  62.          LargeChange     =   16
  63.          Left            =   885
  64.          Max             =   255
  65.          TabIndex        =   6
  66.          Top             =   360
  67.          Width           =   6090
  68.       End
  69.       Begin VB.HScrollBar RedScroll 
  70.          Enabled         =   0   'False
  71.          Height          =   255
  72.          LargeChange     =   16
  73.          Left            =   885
  74.          Max             =   255
  75.          TabIndex        =   5
  76.          Top             =   0
  77.          Width           =   6090
  78.       End
  79.       Begin VB.Label BlueLabel 
  80.          BorderStyle     =   1  'Fixed Single
  81.          Caption         =   "0"
  82.          Height          =   255
  83.          Left            =   480
  84.          TabIndex        =   13
  85.          Top             =   720
  86.          Width           =   375
  87.       End
  88.       Begin VB.Label GreenLabel 
  89.          BorderStyle     =   1  'Fixed Single
  90.          Caption         =   "0"
  91.          Height          =   255
  92.          Left            =   480
  93.          TabIndex        =   12
  94.          Top             =   360
  95.          Width           =   375
  96.       End
  97.       Begin VB.Label RedLabel 
  98.          BorderStyle     =   1  'Fixed Single
  99.          Caption         =   "0"
  100.          Height          =   255
  101.          Left            =   480
  102.          TabIndex        =   11
  103.          Top             =   0
  104.          Width           =   375
  105.       End
  106.       Begin VB.Label Label1 
  107.          Caption         =   "Red"
  108.          Height          =   255
  109.          Index           =   2
  110.          Left            =   0
  111.          TabIndex        =   10
  112.          Top             =   0
  113.          Width           =   495
  114.       End
  115.       Begin VB.Label Label1 
  116.          Caption         =   "Green"
  117.          Height          =   255
  118.          Index           =   1
  119.          Left            =   0
  120.          TabIndex        =   9
  121.          Top             =   360
  122.          Width           =   495
  123.       End
  124.       Begin VB.Label Label1 
  125.          Caption         =   "Blue"
  126.          Height          =   255
  127.          Index           =   0
  128.          Left            =   0
  129.          TabIndex        =   8
  130.          Top             =   720
  131.          Width           =   495
  132.       End
  133.    End
  134.    Begin VB.PictureBox HiddenPict 
  135.       AutoRedraw      =   -1  'True
  136.       Height          =   495
  137.       Left            =   3720
  138.       Picture         =   "PALEDIT.frx":088C
  139.       ScaleHeight     =   29
  140.       ScaleMode       =   3  'Pixel
  141.       ScaleWidth      =   29
  142.       TabIndex        =   0
  143.       Top             =   4560
  144.       Visible         =   0   'False
  145.       Width           =   495
  146.    End
  147.    Begin VB.HScrollBar HBar 
  148.       Height          =   255
  149.       Left            =   0
  150.       TabIndex        =   3
  151.       Top             =   4530
  152.       Width           =   4245
  153.    End
  154.    Begin VB.VScrollBar VBar 
  155.       Height          =   4515
  156.       Left            =   4260
  157.       TabIndex        =   2
  158.       Top             =   0
  159.       Width           =   255
  160.    End
  161.    Begin VB.PictureBox ImagePict 
  162.       AutoRedraw      =   -1  'True
  163.       Height          =   4515
  164.       Left            =   0
  165.       MousePointer    =   2  'Cross
  166.       Picture         =   "PALEDIT.frx":0CD2
  167.       ScaleHeight     =   297
  168.       ScaleMode       =   3  'Pixel
  169.       ScaleWidth      =   279
  170.       TabIndex        =   1
  171.       Top             =   0
  172.       Width           =   4245
  173.    End
  174.    Begin MSComDlg.CommonDialog FileDialog 
  175.       Left            =   4200
  176.       Top             =   4560
  177.       _Version        =   65536
  178.       _ExtentX        =   847
  179.       _ExtentY        =   847
  180.       _StockProps     =   0
  181.       CancelError     =   -1  'True
  182.       FontSize        =   8.37851e-39
  183.    End
  184.    Begin VB.Menu mnuFile 
  185.       Caption         =   "&File"
  186.       Begin VB.Menu mnuFileLoad 
  187.          Caption         =   "&Load..."
  188.          Shortcut        =   ^L
  189.       End
  190.       Begin VB.Menu mnuFileSave 
  191.          Caption         =   "&Save"
  192.          Enabled         =   0   'False
  193.          Shortcut        =   ^S
  194.       End
  195.       Begin VB.Menu mnuFileSaveAs 
  196.          Caption         =   "Save &As..."
  197.          Shortcut        =   ^A
  198.       End
  199.       Begin VB.Menu mnuFileSep1 
  200.          Caption         =   "-"
  201.       End
  202.       Begin VB.Menu mnuFileRevert 
  203.          Caption         =   "&Revert"
  204.          Enabled         =   0   'False
  205.          Shortcut        =   ^R
  206.       End
  207.       Begin VB.Menu mnuFileSep2 
  208.          Caption         =   "-"
  209.       End
  210.       Begin VB.Menu mnuFileExit 
  211.          Caption         =   "E&xit"
  212.       End
  213.    End
  214.    Begin VB.Menu mnuScale 
  215.       Caption         =   "&Scale"
  216.       Begin VB.Menu mnuScaleZoomIn 
  217.          Caption         =   "Zoom &In"
  218.          Shortcut        =   ^I
  219.       End
  220.       Begin VB.Menu mnuScaleFull 
  221.          Caption         =   "&Full Scale"
  222.       End
  223.       Begin VB.Menu mnuScaleZoomOut 
  224.          Caption         =   "Zoom &Out"
  225.          Shortcut        =   ^O
  226.       End
  227.    End
  228.    Begin VB.Menu mnuColor 
  229.       Caption         =   "&Color"
  230.       Begin VB.Menu mnuNear 
  231.          Caption         =   "&Nearest"
  232.          Begin VB.Menu mnuNearRed 
  233.             Caption         =   "&Red"
  234.          End
  235.          Begin VB.Menu mnuNearGreen 
  236.             Caption         =   "&Green"
  237.          End
  238.          Begin VB.Menu mnuNearBlue 
  239.             Caption         =   "&Blue"
  240.          End
  241.          Begin VB.Menu mnuNearGray 
  242.             Caption         =   "Gray"
  243.          End
  244.       End
  245.       Begin VB.Menu mnuGrad 
  246.          Caption         =   "&Gradient"
  247.          Begin VB.Menu mnuGradRed 
  248.             Caption         =   "&Red"
  249.          End
  250.          Begin VB.Menu mnuGradGreen 
  251.             Caption         =   "&Green"
  252.          End
  253.          Begin VB.Menu mnuGradBlue 
  254.             Caption         =   "&Blue"
  255.          End
  256.          Begin VB.Menu mnuGradGray 
  257.             Caption         =   "Gray"
  258.          End
  259.          Begin VB.Menu mnuGradRainbow 
  260.             Caption         =   "Rainbow"
  261.          End
  262.       End
  263.    End
  264. End
  265. Attribute VB_Name = "PalEditForm"
  266. Attribute VB_Creatable = False
  267. Attribute VB_Exposed = False
  268. Option Explicit
  269.  
  270. Const NO_COLOR = -1
  271.  
  272. Dim LogicalPalette As Long
  273. Dim SystemPalette As Long
  274.  
  275. Dim SysPalSize As Integer
  276. Dim NumStaticColors As Integer
  277. Dim StaticColor1 As Integer
  278. Dim StaticColor2 As Integer
  279.  
  280. Dim SelectedI As Integer
  281. Dim SelectedJ As Integer
  282. Dim SelectedColor As Integer
  283. Dim SelectedR As Integer
  284. Dim SelectedG As Integer
  285. Dim SelectedB As Integer
  286.  
  287. Dim Dx As Integer
  288. Dim Dy As Integer
  289. Dim SWid As Single
  290. Dim SHgt As Single
  291. Dim IWid As Single
  292. Dim IHgt As Single
  293. Dim ImageScale As Single
  294.  
  295. Dim SettingColor As Boolean
  296. Dim DataChanged As Boolean
  297. Dim FileLoaded As String
  298. ' ***********************************************
  299. ' If the data has been modified, allow the user
  300. ' to save the changes or cancel the operation.
  301. ' Return True if:
  302. '
  303. '   - The image data has not been changed since
  304. '       it was loaded.
  305. '   - The user saves the changes.
  306. '   - The user says not to save.
  307. '
  308. ' Return False otherwise.
  309. ' ***********************************************
  310. Function DataSafe() As Boolean
  311.     DataSafe = True
  312.     
  313.     ' This is done in a while loop in case the
  314.     ' user starts a save and then cancels.
  315.     Do While DataChanged
  316.         Select Case MsgBox("The data has been modified. Do you want to save the changes?", vbQuestion + vbYesNoCancel, "Data Modified")
  317.             Case vbYes
  318.                 If FileLoaded <> "" Then
  319.                     mnuFileSave_Click
  320.                 Else
  321.                     mnuFileSaveAs_Click
  322.                 End If
  323.                 DataSafe = Not DataChanged
  324.             
  325.             Case vbNo
  326.                 DataSafe = True
  327.                 Exit Do
  328.  
  329.             Case vbCancel
  330.                 DataSafe = False
  331.                 Exit Do
  332.         End Select
  333.     Loop
  334. End Function
  335.  
  336. ' ***********************************************
  337. ' Copy the image from HiddenPict to ImagePict at
  338. ' the correct scale.
  339. ' ***********************************************
  340. Sub DrawImage()
  341. Dim image_wid As Single
  342. Dim image_hgt As Single
  343. Dim hidden_wid As Single
  344. Dim hidden_hgt As Single
  345.  
  346.     If Not Visible Then Exit Sub
  347.  
  348.     ' Fill it with white. Cls would redisplay the
  349.     ' Picture which is bad if ImageScale < 1.
  350.     ImagePict.Line (0, 0)-(IWid, IHgt), vbWhite, BF
  351.         
  352.     ' Copy the picture at the correct scale.
  353.     image_wid = ImagePict.ScaleWidth
  354.     image_hgt = ImagePict.ScaleHeight
  355.     hidden_wid = image_wid / ImageScale
  356.     hidden_hgt = image_hgt / ImageScale
  357.     ImagePict.PaintPicture _
  358.         HiddenPict.Picture, 0, 0, _
  359.         image_wid, image_hgt, _
  360.         HBar.Value, VBar.Value, _
  361.         hidden_wid, hidden_hgt
  362. End Sub
  363.  
  364. ' ***********************************************
  365. ' Load the indicated file and prepare to work
  366. ' with its palette.
  367. ' ***********************************************
  368. Sub LoadImagePict(fname As String)
  369.     On Error GoTo LoadFileError
  370.     HiddenPict.Picture = LoadPicture(fname)
  371.     ImageScale = 1#
  372.     ResetScrollBars
  373.     
  374.     On Error GoTo LoadPalError
  375.     LoadLogicalPalette
  376.  
  377.     FileLoaded = fname
  378.     Caption = "PalEdit [" & fname & "]"
  379.     mnuFileSave.Enabled = True
  380.     mnuFileRevert.Enabled = True
  381.     DataChanged = False
  382.     Exit Sub
  383.     
  384. LoadFileError:
  385.     Beep
  386.     MsgBox "Error loading file " & fname & "." & _
  387.         vbCrLf & Error$
  388.     Exit Sub
  389.  
  390. LoadPalError:
  391.     Beep
  392.     MsgBox "Error loading logical palette." & _
  393.         vbCrLf & Error$
  394.     Exit Sub
  395. End Sub
  396.  
  397. ' ***********************************************
  398. ' Set the Max and LargeChange properties for the
  399. ' image scroll bars.
  400. ' ***********************************************
  401. Sub ResetScrollBars()
  402. Dim change As Single
  403.  
  404.     change = ImagePict.ScaleWidth / ImageScale
  405.     If HiddenPict.ScaleWidth <= change Then
  406.         HBar.Value = 0
  407.         HBar.Enabled = False
  408.     Else
  409.         HBar.Max = HiddenPict.ScaleWidth - change
  410.         HBar.LargeChange = change
  411.         HBar.Enabled = True
  412.     End If
  413.     
  414.     change = ImagePict.ScaleHeight / ImageScale
  415.     If HiddenPict.ScaleHeight <= change Then
  416.         VBar.Value = 0
  417.         VBar.Enabled = False
  418.     Else
  419.         VBar.Max = HiddenPict.ScaleHeight - change
  420.         VBar.LargeChange = change
  421.         VBar.Enabled = True
  422.     End If
  423. End Sub
  424.  
  425. ' ***********************************************
  426. ' Select the color with the indicated index.
  427. ' ***********************************************
  428. Sub SelectColorIndex(ByVal index As Integer)
  429. Dim i As Integer
  430. Dim j As Integer
  431.  
  432.     i = index \ 16
  433.     j = index Mod 16
  434.     SelectColor i, j
  435. End Sub
  436.  
  437. ' ***********************************************
  438. ' Give the form and all the picture boxes an
  439. ' hourglass cursor.
  440. ' ***********************************************
  441. Sub WaitStart()
  442.     MousePointer = vbHourglass
  443.     SystemColors.MousePointer = vbHourglass
  444.     ImagePict.MousePointer = vbHourglass
  445.     ColorSwatch.MousePointer = vbHourglass
  446.     DoEvents
  447. End Sub
  448.  
  449.  
  450. ' ***********************************************
  451. ' Restore the mouse pointers for the form and all
  452. ' the picture boxes.
  453. ' ***********************************************
  454. Sub WaitEnd()
  455.     MousePointer = vbDefault
  456.     SystemColors.MousePointer = vbDefault
  457.     ImagePict.MousePointer = vbCrosshair
  458.     ColorSwatch.MousePointer = vbDefault
  459. End Sub
  460.  
  461. ' ***********************************************
  462. ' Load the HiddenPict palette so its entries
  463. ' match the system entries.
  464. ' ***********************************************
  465. Sub LoadLogicalPalette()
  466. Dim palentry(0 To 255) As PALETTEENTRY
  467. Dim blanked(0 To 255) As PALETTEENTRY
  468. Dim i As Integer
  469.  
  470.     ' Make ImagePict and ColorSwatch use the same
  471.     ' palette as HiddenPict.
  472.     ImagePict.Picture = HiddenPict.Picture
  473.     ColorSwatch.Picture = HiddenPict.Picture
  474.     LogicalPalette = HiddenPict.Picture.hPal
  475.     
  476.     ' Draw the image at the correct scale.
  477.     DrawImage
  478.     
  479.     ' Make sure ImagePict has the foreground palette.
  480.     i = RealizePalette(ImagePict.hdc)
  481.  
  482.     ' Give the system a chance to catch up.
  483.     DoEvents
  484.  
  485.     ' Make the logical palette as big as possible.
  486.     If ResizePalette(LogicalPalette, SysPalSize) = 0 Then
  487.         Beep
  488.         MsgBox "Error resizing logical palette.", _
  489.             vbExclamation
  490.         Exit Sub
  491.     End If
  492.     
  493.     ' Get the system palette entries.
  494.     i = GetSystemPaletteEntries(HiddenPict.hdc, 0, SysPalSize, palentry(0))
  495.     
  496.     ' Blank the non-static colors.
  497.     For i = 0 To StaticColor1
  498.         blanked(i) = palentry(i)
  499.     Next i
  500.     For i = StaticColor1 + 1 To StaticColor2 - 1
  501.         With blanked(i)
  502.             .peRed = 0
  503.             .peGreen = 0
  504.             .peBlue = 0
  505.             .peFlags = PC_NOCOLLAPSE
  506.         End With
  507.     Next i
  508.     For i = StaticColor2 To 255
  509.         blanked(i) = palentry(i)
  510.     Next i
  511.     i = SetPaletteEntries(LogicalPalette, 0, SysPalSize, blanked(0))
  512.  
  513.     ' Insert the non-static colors.
  514.     For i = StaticColor1 + 1 To StaticColor2 - 1
  515.         palentry(i).peFlags = PC_NOCOLLAPSE
  516.     Next i
  517.     i = SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  518.     
  519.     ' Realize the new palette values.
  520.     i = RealizePalette(ImagePict.hdc)
  521.  
  522.     ' Select the color that was selected before.
  523.     SelectColor SelectedI, SelectedJ
  524. End Sub
  525.  
  526. ' ***********************************************
  527. ' Load the SystemColors palette with PC_EXPLICIT
  528. ' entries so they match the system palette.
  529. ' ***********************************************
  530. Sub LoadSystemPalette()
  531. Dim palentry(0 To 255) As PALETTEENTRY
  532. Dim i As Integer
  533.  
  534.     ' Make the logical palette as big as possible.
  535.     SystemPalette = SystemColors.Picture.hPal
  536.     If ResizePalette(SystemPalette, SysPalSize) = 0 Then
  537.         Beep
  538.         MsgBox "Error resizing system palette.", _
  539.             vbExclamation
  540.         Exit Sub
  541.     End If
  542.     
  543.     ' Flag all palette entries as PC_EXPLICIT.
  544.     ' Set peRed to the system palette indexes.
  545.     For i = 0 To SysPalSize - 1
  546.         palentry(i).peRed = i
  547.         palentry(i).peFlags = PC_EXPLICIT
  548.     Next i
  549.     
  550.     ' Update the palette (ignore return value).
  551.     i = SetPaletteEntries(SystemPalette, 0, SysPalSize, palentry(0))
  552. End Sub
  553.  
  554.  
  555. ' ***********************************************
  556. ' Fill the system picture with all the palette
  557. ' colors, hatching the static colors.
  558. ' ***********************************************
  559. Sub ShowSystemColors()
  560. Dim i As Integer
  561. Dim j As Integer
  562. Dim clr As Integer
  563. Dim oldfill As Integer
  564. Dim olddraw As Integer
  565.  
  566.     SystemColors.Cls
  567.     
  568.     ' Display the colors using palette indexing.
  569.     Dx = SystemColors.ScaleWidth / 16
  570.     Dy = SystemColors.ScaleHeight / 16
  571.     clr = 0
  572.     For i = 0 To 15
  573.         For j = 0 To 15
  574.             SystemColors.Line _
  575.                 (j * Dx, i * Dy)-Step(Dx, Dy), _
  576.                 clr + &H1000000, BF
  577.             clr = clr + 1
  578.         Next j
  579.     Next i
  580.     
  581.     ' Hatch the static colors.
  582.     oldfill = SystemColors.FillStyle
  583.     olddraw = SystemColors.DrawMode
  584.     SystemColors.FillStyle = vbDownwardDiagonal
  585.     SystemColors.DrawMode = vbInvisible
  586.     
  587.     SystemColors.Line (0, 0)-Step((NumStaticColors \ 2) * Dx - 1, Dy - 1), , B
  588.     SystemColors.Line (16 * Dx, 16 * Dy)-Step(-(NumStaticColors \ 2) * Dx, -Dy), , B
  589.     
  590.     SystemColors.FillStyle = oldfill
  591.     SystemColors.DrawMode = olddraw
  592.  
  593.     ' Highlight color (0, 0).
  594.     SelectedColor = NO_COLOR
  595.     SelectColor 0, 0
  596. End Sub
  597.  
  598. ' ***********************************************
  599. ' Select the color at the indicated position.
  600. ' ***********************************************
  601. Sub SelectColor(ByVal i As Integer, ByVal j As Integer)
  602. Const GAP1 = 1
  603. Const GAP2 = 2
  604. Const DRAW_WID = 2
  605.  
  606. Dim oldmode As Integer
  607. Dim oldwid As Integer
  608.  
  609.     oldmode = SystemColors.DrawMode
  610.     oldwid = SystemColors.DrawWidth
  611.     SystemColors.DrawMode = vbInvert
  612.     SystemColors.DrawWidth = DRAW_WID
  613.     
  614.     ' Unhighlight the previously selected color.
  615.     If SelectedColor <> NO_COLOR Then _
  616.         SystemColors.Line (SelectedJ * Dx + GAP1, SelectedI * Dx + GAP1)-Step(Dx - GAP2, Dx - GAP2), , B
  617.     
  618.     ' Record the new color.
  619.     SelectedI = i
  620.     SelectedJ = j
  621.     SelectedColor = i * 16 + j
  622.  
  623.     ' Highlight the new color.
  624.     SystemColors.Line (SelectedJ * Dx + GAP1, SelectedI * Dx + GAP1)-Step(Dx - GAP2, Dx - GAP2), , B
  625.     SystemColors.DrawMode = oldmode
  626.     SystemColors.DrawWidth = oldwid
  627.  
  628.     ' Display the color's components.
  629.     ShowColorValue
  630. End Sub
  631.  
  632.  
  633. ' ***********************************************
  634. ' Display the selected color's components in the
  635. ' colors labels and scroll bars.
  636. ' ***********************************************
  637. Sub ShowColorValue()
  638. Dim palentry As PALETTEENTRY
  639. Dim status As Integer
  640.  
  641.     If SelectedColor = NO_COLOR Then Exit Sub
  642.     
  643.     status = GetSystemPaletteEntries(SystemColors.hdc, SelectedColor, 1, palentry)
  644.     
  645.     ' Update the labels.
  646.     RedLabel.Caption = Format$(palentry.peRed)
  647.     GreenLabel.Caption = Format$(palentry.peGreen)
  648.     BlueLabel.Caption = Format$(palentry.peBlue)
  649.     
  650.     ' Update the color swatch.
  651.     ColorSwatch.Line (0, 0)-(SWid, SHgt), RGB(palentry.peRed, palentry.peGreen, palentry.peBlue), BF
  652.  
  653.     ' Update the scroll bars.
  654.     If SelectedColor > StaticColor1 And SelectedColor < StaticColor2 Then
  655.         SettingColor = True
  656.         RedScroll.Value = palentry.peRed
  657.         GreenScroll.Value = palentry.peGreen
  658.         BlueScroll.Value = palentry.peBlue
  659.         SettingColor = False
  660.         RedScroll.Enabled = True
  661.         GreenScroll.Enabled = True
  662.         BlueScroll.Enabled = True
  663.     Else
  664.         RedScroll.Enabled = False
  665.         GreenScroll.Enabled = False
  666.         BlueScroll.Enabled = False
  667.     End If
  668. End Sub
  669.  
  670.  
  671. ' ***********************************************
  672. ' Update the selected color's value.
  673. ' ***********************************************
  674. Sub UpdatePalette()
  675. Dim pe As PALETTEENTRY
  676. Dim i As Integer
  677.  
  678.     pe.peRed = RedScroll.Value
  679.     pe.peGreen = GreenScroll.Value
  680.     pe.peBlue = BlueScroll.Value
  681.     pe.peFlags = PC_NOCOLLAPSE
  682.  
  683.     i = SetPaletteEntries(LogicalPalette, SelectedColor, 1, pe)
  684.     i = RealizePalette(HiddenPict.hdc)
  685.  
  686.     ColorSwatch.Line (0, 0)-(SWid, SHgt), RGB(pe.peRed, pe.peGreen, pe.peBlue), BF
  687.  
  688.     DataChanged = True
  689. End Sub
  690.  
  691.  
  692.  
  693.  
  694. ' ***********************************************
  695. ' Update the selected color's value.
  696. ' ***********************************************
  697. Private Sub BlueScroll_Change()
  698.     If SettingColor Then Exit Sub
  699.     BlueLabel.Caption = Format$(BlueScroll.Value)
  700.     UpdatePalette
  701. End Sub
  702.  
  703.  
  704. ' ***********************************************
  705. ' Update the selected color's value.
  706. ' ***********************************************
  707. Private Sub BlueScroll_Scroll()
  708.     If SettingColor Then Exit Sub
  709.     BlueLabel.Caption = Format$(BlueScroll.Value)
  710.     UpdatePalette
  711. End Sub
  712.  
  713.  
  714. ' ***********************************************
  715. ' Make the scroll bars as big as possible within
  716. ' ColorBox.
  717. ' ***********************************************
  718. Private Sub ColorBox_Resize()
  719. Dim wid As Single
  720.  
  721.     wid = ColorBox.ScaleWidth - RedLabel.Left - RedLabel.Width - 2
  722.     If wid < 10 Then wid = 10
  723.     RedScroll.Width = wid
  724.     GreenScroll.Width = wid
  725.     BlueScroll.Width = wid
  726. End Sub
  727.  
  728.  
  729. ' ***********************************************
  730. ' 1. Make sure we can handle palettes.
  731. ' 2. Find out how big the system palette is and how
  732. ' many static colors there are.
  733. ' 3. Load and display the system palette.
  734. ' ***********************************************
  735. Private Sub Form_Load()
  736.     ' Make sure the screen supports palettes.
  737.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  738.         Beep
  739.         MsgBox "This monitor does not support palettes.", _
  740.             vbCritical
  741.         End
  742.     End If
  743.  
  744.     ' get system palette size and # static colors.
  745.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  746.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  747.     StaticColor1 = NumStaticColors \ 2 - 1
  748.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  749.  
  750.     HiddenPict.AutoSize = True
  751.     ImageScale = 1#
  752.     
  753.     ' Load the system palette.
  754.     LoadSystemPalette
  755.  
  756.     ' Display the system palette.
  757.     ShowSystemColors
  758.     
  759.     ' Load the logical palette.
  760.     LoadLogicalPalette
  761. End Sub
  762.  
  763. ' ***********************************************
  764. ' Refuse to unload if there are unsaved changes.
  765. ' ***********************************************
  766. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  767.     Cancel = Not DataSafe()
  768. End Sub
  769.  
  770.  
  771. ' ***********************************************
  772. ' Make the picture as large as possible.
  773. ' ***********************************************
  774. Private Sub Form_Resize()
  775. Dim L As Single
  776. Dim T As Single
  777. Dim wid As Single
  778. Dim hgt As Single
  779.  
  780.     If WindowState = vbMinimized Then Exit Sub
  781.     
  782.     ' Keep system colors in the upper right corner.
  783.     SystemColors.Move ScaleWidth - SystemColors.Width
  784.     
  785.     ' Keep color box stretched across the bottom.
  786.     ColorBox.Move 0, ScaleHeight - ColorBox.Height, ScaleWidth
  787.     
  788.     ' Put color swatch under system colors.
  789.     hgt = ColorBox.Top - SystemColors.Height - 6
  790.     If hgt < 10 Then hgt = 10
  791.     ColorSwatch.Move SystemColors.Left, SystemColors.Height + 3, ColorSwatch.Width, hgt
  792.     SWid = ColorSwatch.ScaleWidth - 1
  793.     SHgt = ColorSwatch.ScaleHeight - 1
  794.     
  795.     ' Place the vertical scroll bar.
  796.     L = SystemColors.Left - VBar.Width - 3
  797.     hgt = ColorBox.Top - HBar.Height - 4
  798.     If hgt < 10 Then hgt = 10
  799.     VBar.Move L, 0, VBar.Width, hgt
  800.     
  801.     ' Place the horizontal scroll bar.
  802.     T = ColorBox.Top - HBar.Height - 3
  803.     wid = SystemColors.Left - VBar.Width - 4
  804.     If wid < 10 Then wid = 10
  805.     HBar.Move 0, T, wid
  806.         
  807.     ' Place ImagePict inside the scroll bars.
  808.     ImagePict.Move 0, 0, wid, hgt
  809.     IWid = ImagePict.ScaleWidth - 1
  810.     IHgt = ImagePict.ScaleHeight - 1
  811.  
  812.     ' Set the scroll bar limits.
  813.     ResetScrollBars
  814.     
  815.     ' Redraw the image in case we've grown.
  816.     DrawImage
  817.     
  818.     ' Refill ColorSwatch (it may have grown).
  819.     ShowColorValue
  820. End Sub
  821.  
  822.  
  823. Private Sub Form_Unload(Cancel As Integer)
  824.     End
  825. End Sub
  826.  
  827. ' ***********************************************
  828. ' Update the selected color's value.
  829. ' ***********************************************
  830. Private Sub GreenScroll_Change()
  831.     If SettingColor Then Exit Sub
  832.     GreenLabel.Caption = Format$(GreenScroll.Value)
  833.     UpdatePalette
  834. End Sub
  835.  
  836. ' ***********************************************
  837. ' Update the selected color's value.
  838. ' ***********************************************
  839. Private Sub GreenScroll_Scroll()
  840.     If SettingColor Then Exit Sub
  841.     GreenLabel.Caption = Format$(GreenScroll.Value)
  842.     UpdatePalette
  843. End Sub
  844.  
  845. ' ***********************************************
  846. ' Redraw the image scrolled appropriately.
  847. ' ***********************************************
  848. Private Sub HBar_Change()
  849.     DrawImage
  850. End Sub
  851.  
  852. ' ***********************************************
  853. ' Redraw the image scrolled appropriately.
  854. ' ***********************************************
  855. Private Sub HBar_Scroll()
  856.     DrawImage
  857. End Sub
  858.  
  859.  
  860. ' ***********************************************
  861. ' Select the color the user clicked on.
  862. ' ***********************************************
  863. Private Sub ImagePict_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  864. Dim bm As BITMAP
  865. Dim hbm As Integer
  866. Dim status As Long
  867. Dim bytes() As Byte
  868. Dim wid As Long
  869. Dim hgt As Long
  870.  
  871.     ' Get a handle to the bitmap.
  872.     hbm = ImagePict.Image
  873.     
  874.     ' See how big it is.
  875.     status = GetObject(hbm, BITMAP_SIZE, bm)
  876.     wid = bm.bmWidthBytes
  877.     hgt = bm.bmHeight
  878.     
  879.     ' If the mouse is out of bounds, bail out.
  880.     If X >= wid Or Y >= hgt Then
  881.         Beep
  882.         Exit Sub
  883.     End If
  884.     
  885.     ' Get the bits.
  886.     ReDim bytes(0 To wid - 1, 0 To hgt - 1)
  887.     status = GetBitmapBits(hbm, wid * hgt, bytes(0, 0))
  888.     
  889.     ' Select the color of this pixel.
  890.     SelectColorIndex bytes(CInt(X), CInt(Y))
  891. End Sub
  892.  
  893.  
  894. ' ***********************************************
  895. ' Load a new image file.
  896. ' ***********************************************
  897. Private Sub mnuFileLoad_Click()
  898. Dim fname As String
  899.  
  900.     ' Make sure any changes have been saved.
  901.     If Not DataSafe() Then Exit Sub
  902.     
  903.     ' Allow the user to pick a file.
  904.     On Error Resume Next
  905.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  906.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  907.     FileDialog.ShowOpen
  908.     If Err.Number = cdlCancel Then
  909.         Exit Sub
  910.     ElseIf Err.Number <> 0 Then
  911.         Beep
  912.         MsgBox "Error selecting file.", , vbExclamation
  913.         Exit Sub
  914.     End If
  915.     On Error GoTo 0
  916.     
  917.     fname = Trim$(FileDialog.filename)
  918.     FileDialog.InitDir = Left$(fname, Len(fname) _
  919.         - Len(FileDialog.FileTitle) - 1)
  920.     
  921.     ' Load the picture.
  922.     WaitStart
  923.     DoEvents
  924.     LoadImagePict fname
  925.     WaitEnd
  926. End Sub
  927.  
  928. ' ***********************************************
  929. ' Reload the file.
  930. ' ***********************************************
  931. Private Sub mnuFileRevert_Click()
  932.     ' If the data has changed, get confirmation.
  933.     If DataChanged Then
  934.         If MsgBox("The data has been modified. Are you sure you want to remove the changes?", _
  935.             vbQuestion + vbYesNo) = vbNo Then _
  936.                 Exit Sub
  937.     End If
  938.  
  939.     ' Reload the picture.
  940.     WaitStart
  941.     DoEvents
  942.     LoadImagePict FileLoaded
  943.     WaitEnd
  944. End Sub
  945.  
  946. ' ***********************************************
  947. ' Save the image in the file from which it was
  948. ' loaded.
  949. ' ***********************************************
  950. Private Sub mnuFileSave_Click()
  951.     WaitStart
  952.     DoEvents
  953.     SaveImagePict FileLoaded
  954.     WaitEnd
  955. End Sub
  956.  
  957. ' ***********************************************
  958. ' Save the image in a new file.
  959. ' ***********************************************
  960. Private Sub mnuFileSaveAs_Click()
  961. Dim fname As String
  962.  
  963.     ' Allow the user to pick a file.
  964.     On Error Resume Next
  965.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  966.     FileDialog.Flags = cdlOFNOverwritePrompt + _
  967.         cdlOFNHideReadOnly + cdlOFNPathMustExist
  968.     FileDialog.ShowSave
  969.     If Err.Number = cdlCancel Then
  970.         Exit Sub
  971.     ElseIf Err.Number <> 0 Then
  972.         Beep
  973.         MsgBox "Error selecting file.", , vbExclamation
  974.         Exit Sub
  975.     End If
  976.     On Error GoTo 0
  977.     
  978.     fname = Trim$(FileDialog.filename)
  979.     FileDialog.InitDir = Left$(fname, Len(fname) _
  980.         - Len(FileDialog.FileTitle) - 1)
  981.  
  982.     ' Save the picture.
  983.     WaitStart
  984.     DoEvents
  985.     SaveImagePict fname
  986.     WaitEnd
  987. End Sub
  988.  
  989.  
  990. ' ***********************************************
  991. ' Save the picture in the indicated file.
  992. ' ***********************************************
  993. Sub SaveImagePict(fname As String)
  994.     On Error GoTo SaveError
  995.     SavePicture HiddenPict.Picture, fname
  996.     
  997.     Caption = "PalEdit [" & fname & "]"
  998.     FileLoaded = fname
  999.     DataChanged = False
  1000.     Exit Sub
  1001.  
  1002. SaveError:
  1003.     Beep
  1004.     MsgBox "Error saving picture in file " & _
  1005.         fname & "." & vbCrLf & vbCrLf & _
  1006.         Error$, , vbExclamation
  1007.     Exit Sub
  1008.  
  1009. End Sub
  1010.  
  1011.  
  1012. ' ***********************************************
  1013. ' Replace colors with a green gradient.
  1014. ' ***********************************************
  1015. Private Sub mnuGradGreen_Click()
  1016. Dim palentry(0 To 255) As PALETTEENTRY
  1017. Dim i As Integer
  1018. Dim g As Single
  1019. Dim Dg As Single
  1020.  
  1021.     Dg = 255 / (StaticColor2 - StaticColor1)
  1022.     g = Dg
  1023.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1024.         With palentry(i)
  1025.             .peRed = 0
  1026.             .peGreen = g
  1027.             .peBlue = 0
  1028.             .peFlags = PC_NOCOLLAPSE
  1029.         End With
  1030.         g = g + Dg
  1031.     Next i
  1032.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1033.         Beep
  1034.         MsgBox "Error resetting colors.", , vbExclamation
  1035.         Exit Sub
  1036.     End If
  1037.     i = RealizePalette(ImagePict.hdc)
  1038.     DataChanged = True
  1039. End Sub
  1040.  
  1041. ' ***********************************************
  1042. ' Replace colors with red, green, and blue
  1043. ' gradients.
  1044. ' ***********************************************
  1045. Private Sub mnuGradRainbow_Click()
  1046. Dim palentry(0 To 255) As PALETTEENTRY
  1047. Dim i As Integer
  1048. Dim num_each As Integer
  1049. Dim clr As Integer
  1050. Dim c As Single
  1051. Dim Dc As Single
  1052.  
  1053.     num_each = (StaticColor2 - StaticColor1) / 3
  1054.     Dc = 255 / num_each
  1055.     clr = StaticColor1 + 1
  1056.     
  1057.     ' Red shades.
  1058.     c = Dc
  1059.     For i = 1 To num_each
  1060.         With palentry(clr)
  1061.             .peRed = c
  1062.             .peGreen = 0
  1063.             .peBlue = 0
  1064.             .peFlags = PC_NOCOLLAPSE
  1065.         End With
  1066.         c = c + Dc
  1067.         clr = clr + 1
  1068.     Next i
  1069.     
  1070.     ' Green shades.
  1071.     c = Dc
  1072.     For i = 1 To num_each
  1073.         With palentry(clr)
  1074.             .peRed = 0
  1075.             .peGreen = c
  1076.             .peBlue = 0
  1077.             .peFlags = PC_NOCOLLAPSE
  1078.         End With
  1079.         c = c + Dc
  1080.         clr = clr + 1
  1081.     Next i
  1082.     
  1083.     ' Blue shades.
  1084.     c = Dc
  1085.     For i = clr To StaticColor2 - 1
  1086.         With palentry(clr)
  1087.             .peRed = 0
  1088.             .peGreen = 0
  1089.             .peBlue = c
  1090.             .peFlags = PC_NOCOLLAPSE
  1091.         End With
  1092.         c = c + Dc
  1093.         clr = clr + 1
  1094.     Next i
  1095.  
  1096.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1097.         Beep
  1098.         MsgBox "Error resetting colors.", , vbExclamation
  1099.         Exit Sub
  1100.     End If
  1101.     i = RealizePalette(ImagePict.hdc)
  1102.     DataChanged = True
  1103. End Sub
  1104.  
  1105. ' ***********************************************
  1106. ' Replace colors with a red gradient.
  1107. ' ***********************************************
  1108. Private Sub mnuGradRed_Click()
  1109. Dim palentry(0 To 255) As PALETTEENTRY
  1110. Dim i As Integer
  1111. Dim r As Single
  1112. Dim Dr As Single
  1113.  
  1114.     Dr = 255 / (StaticColor2 - StaticColor1)
  1115.     r = Dr
  1116.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1117.         With palentry(i)
  1118.             .peRed = r
  1119.             .peGreen = 0
  1120.             .peBlue = 0
  1121.             .peFlags = PC_NOCOLLAPSE
  1122.         End With
  1123.         r = r + Dr
  1124.     Next i
  1125.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1126.         Beep
  1127.         MsgBox "Error resetting colors.", , vbExclamation
  1128.         Exit Sub
  1129.     End If
  1130.     i = RealizePalette(ImagePict.hdc)
  1131.     DataChanged = True
  1132. End Sub
  1133. ' ***********************************************
  1134. ' Replace colors with appropriate greens.
  1135. ' ***********************************************
  1136. Private Sub mnuNearGreen_Click()
  1137. Dim palentry(0 To 255) As PALETTEENTRY
  1138. Dim i As Integer
  1139. Dim clr As Integer
  1140.  
  1141.     ' Get the current color values.
  1142.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  1143.  
  1144.     ' Fill in the nearest shades.
  1145.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1146.         With palentry(i)
  1147.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  1148.             .peRed = 0
  1149.             .peGreen = clr
  1150.             .peBlue = 0
  1151.             .peFlags = PC_NOCOLLAPSE
  1152.         End With
  1153.     Next i
  1154.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1155.         Beep
  1156.         MsgBox "Error resetting colors.", , vbExclamation
  1157.         Exit Sub
  1158.     End If
  1159.     i = RealizePalette(ImagePict.hdc)
  1160.     DataChanged = True
  1161. End Sub
  1162.  
  1163.  
  1164. ' ***********************************************
  1165. ' Replace colors with appropriate reds.
  1166. ' ***********************************************
  1167. Private Sub mnuNearRed_Click()
  1168. Dim palentry(0 To 255) As PALETTEENTRY
  1169. Dim i As Integer
  1170. Dim clr As Integer
  1171.  
  1172.     ' Get the current color values.
  1173.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  1174.  
  1175.     ' Fill in the nearest shades.
  1176.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1177.         With palentry(i)
  1178.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  1179.             .peRed = clr
  1180.             .peGreen = 0
  1181.             .peBlue = 0
  1182.             .peFlags = PC_NOCOLLAPSE
  1183.         End With
  1184.     Next i
  1185.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1186.         Beep
  1187.         MsgBox "Error resetting colors.", , vbExclamation
  1188.         Exit Sub
  1189.     End If
  1190.     i = RealizePalette(ImagePict.hdc)
  1191.     DataChanged = True
  1192. End Sub
  1193.  
  1194.  
  1195.  
  1196. ' ***********************************************
  1197. ' Replace colors with appropriate grays.
  1198. ' ***********************************************
  1199. Private Sub mnuNearGray_Click()
  1200. Dim palentry(0 To 255) As PALETTEENTRY
  1201. Dim i As Integer
  1202. Dim clr As Integer
  1203.  
  1204.     ' Get the current color values.
  1205.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  1206.  
  1207.     ' Fill in the nearest shades.
  1208.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1209.         With palentry(i)
  1210.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  1211.             .peRed = clr
  1212.             .peGreen = clr
  1213.             .peBlue = clr
  1214.             .peFlags = PC_NOCOLLAPSE
  1215.         End With
  1216.     Next i
  1217.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1218.         Beep
  1219.         MsgBox "Error resetting colors.", , vbExclamation
  1220.         Exit Sub
  1221.     End If
  1222.     i = RealizePalette(ImagePict.hdc)
  1223.     DataChanged = True
  1224. End Sub
  1225.  
  1226.  
  1227.  
  1228. ' ***********************************************
  1229. ' Replace colors with appropriate blues.
  1230. ' ***********************************************
  1231. Private Sub mnuNearBlue_Click()
  1232. Dim palentry(0 To 255) As PALETTEENTRY
  1233. Dim i As Integer
  1234. Dim clr As Integer
  1235.  
  1236.     ' Get the current color values.
  1237.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  1238.  
  1239.     ' Fill in the nearest shades.
  1240.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1241.         With palentry(i)
  1242.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  1243.             .peRed = 0
  1244.             .peGreen = 0
  1245.             .peBlue = clr
  1246.             .peFlags = PC_NOCOLLAPSE
  1247.         End With
  1248.     Next i
  1249.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1250.         Beep
  1251.         MsgBox "Error resetting colors.", , vbExclamation
  1252.         Exit Sub
  1253.     End If
  1254.     i = RealizePalette(ImagePict.hdc)
  1255.     DataChanged = True
  1256. End Sub
  1257.  
  1258.  
  1259.  
  1260. ' ***********************************************
  1261. ' Replace colors with a gray gradient.
  1262. ' ***********************************************
  1263. Private Sub mnuGradGray_Click()
  1264. Dim palentry(0 To 255) As PALETTEENTRY
  1265. Dim i As Integer
  1266. Dim g As Single
  1267. Dim Dg As Single
  1268.  
  1269.     Dg = 255 / (StaticColor2 - StaticColor1)
  1270.     g = Dg
  1271.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1272.         With palentry(i)
  1273.             .peRed = g
  1274.             .peGreen = g
  1275.             .peBlue = g
  1276.             .peFlags = PC_NOCOLLAPSE
  1277.         End With
  1278.         g = g + Dg
  1279.     Next i
  1280.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1281.         Beep
  1282.         MsgBox "Error resetting colors.", , vbExclamation
  1283.         Exit Sub
  1284.     End If
  1285.     i = RealizePalette(ImagePict.hdc)
  1286.     DataChanged = True
  1287. End Sub
  1288.  
  1289.  
  1290. ' ***********************************************
  1291. ' Replace colors with a blue gradient.
  1292. ' ***********************************************
  1293. Private Sub mnuGradBlue_Click()
  1294. Dim palentry(0 To 255) As PALETTEENTRY
  1295. Dim i As Integer
  1296. Dim b As Single
  1297. Dim Db As Single
  1298.  
  1299.     Db = 255 / (StaticColor2 - StaticColor1)
  1300.     b = Db
  1301.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1302.         With palentry(i)
  1303.             .peRed = 0
  1304.             .peGreen = 0
  1305.             .peBlue = b
  1306.             .peFlags = PC_NOCOLLAPSE
  1307.         End With
  1308.         b = b + Db
  1309.     Next i
  1310.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1311.         Beep
  1312.         MsgBox "Error resetting colors.", , vbExclamation
  1313.         Exit Sub
  1314.     End If
  1315.     i = RealizePalette(ImagePict.hdc)
  1316.     DataChanged = True
  1317. End Sub
  1318.  
  1319.  
  1320. ' ***********************************************
  1321. ' Set ImageScale = 1 and redraw the image.
  1322. ' ***********************************************
  1323. Private Sub mnuScaleFull_Click()
  1324.     ImageScale = 1#
  1325.     ResetScrollBars
  1326.     DrawImage
  1327. End Sub
  1328. ' ***********************************************
  1329. ' Increase ImageScale and redraw the image.
  1330. ' ***********************************************
  1331. Private Sub mnuScaleZoomIn_Click()
  1332.     ImageScale = ImageScale * 2#
  1333.     ResetScrollBars
  1334.     DrawImage
  1335. End Sub
  1336.  
  1337.  
  1338. ' ***********************************************
  1339. ' Decrease ImageScale and redraw the image.
  1340. ' ***********************************************
  1341. Private Sub mnuScaleZoomOut_Click()
  1342.     ImageScale = ImageScale / 2#
  1343.     ResetScrollBars
  1344.     DrawImage
  1345. End Sub
  1346.  
  1347.  
  1348. ' ***********************************************
  1349. ' Update the selected color's value.
  1350. ' ***********************************************
  1351. Private Sub RedScroll_Change()
  1352.     If SettingColor Then Exit Sub
  1353.     RedLabel.Caption = Format$(RedScroll.Value)
  1354.     UpdatePalette
  1355. End Sub
  1356.  
  1357.  
  1358. ' ***********************************************
  1359. ' Update the selected color's value.
  1360. ' ***********************************************
  1361. Private Sub RedScroll_Scroll()
  1362.     If SettingColor Then Exit Sub
  1363.     RedLabel.Caption = Format$(RedScroll.Value)
  1364.     UpdatePalette
  1365. End Sub
  1366.  
  1367. ' ***********************************************
  1368. ' Select the color the user clicked on.
  1369. ' ***********************************************
  1370. Private Sub SystemColors_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  1371. Dim i As Integer
  1372. Dim j As Integer
  1373.  
  1374.     i = Y \ Dx
  1375.     j = X \ Dy
  1376.     SelectColor i, j
  1377. End Sub
  1378.  
  1379. ' ***********************************************
  1380. ' End the application. (See also the QueryUnload
  1381. ' event.)
  1382. ' ***********************************************
  1383. Private Sub mnuFileExit_Click()
  1384.     Unload Me
  1385. End Sub
  1386.  
  1387.  
  1388. ' ***********************************************
  1389. ' Allow the user to select a new color with the
  1390. ' arrow keys.
  1391. ' ***********************************************
  1392. Private Sub SystemColors_KeyDown(KeyCode As Integer, Shift As Integer)
  1393. Dim i As Integer
  1394. Dim j As Integer
  1395.  
  1396.     i = SelectedI
  1397.     j = SelectedJ
  1398.  
  1399.     Select Case KeyCode
  1400.         Case vbKeyDown
  1401.             i = i + 1
  1402.             If i * 16 + j >= SysPalSize Then i = 0
  1403.         
  1404.         Case vbKeyUp
  1405.             i = i - 1
  1406.             If i < 0 Then
  1407.                 i = (SysPalSize - 1) \ 16
  1408.                 If i * 16 + j >= SysPalSize Then _
  1409.                     i = i - 1
  1410.             End If
  1411.         
  1412.         Case vbKeyLeft
  1413.             j = j - 1
  1414.             If j < 0 Then
  1415.                 j = 15
  1416.                 If i * 16 + j >= SysPalSize Then _
  1417.                     j = SysPalSize - 1 - i * 16
  1418.             End If
  1419.         
  1420.         Case vbKeyRight
  1421.             j = j + 1
  1422.             If j > 15 Or _
  1423.                 i * 16 + j >= SysPalSize Then _
  1424.                     j = 0
  1425.         
  1426.     End Select
  1427.     
  1428.     SelectColor i, j
  1429. End Sub
  1430.  
  1431.  
  1432.  
  1433. ' ***********************************************
  1434. ' Redraw the image scrolled appropriately.
  1435. ' ***********************************************
  1436. Private Sub VBar_Change()
  1437.     DrawImage
  1438. End Sub
  1439.  
  1440.  
  1441. ' ***********************************************
  1442. ' Redraw the image scrolled appropriately.
  1443. ' ***********************************************
  1444. Private Sub VBar_Scroll()
  1445.     DrawImage
  1446. End Sub
  1447.  
  1448.  
  1449.